package 第二十章项目.clothesSystem.ui;

import 第二十章项目.clothesSystem.bean.User;
import 第二十章项目.clothesSystem.utils.BusinessException;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class UserIO extends BaseClass{
    private static List<User> users=new ArrayList<>();
    private static List<User> tmp = new ArrayList<>();
    private static final String USER_FILE="user.obj";

    static {
        try {
            ObjectInputStream in = new ObjectInputStream
                    (new FileInputStream(USER_FILE));
            tmp=(List<User>)in.readObject();
            in.close();
        } catch (BusinessException|IOException | ClassNotFoundException e) {
            println(getString("tmp.loading.error"));
        }

    }

    public boolean writeUsers()throws BusinessException {
        try {
            ObjectOutputStream out;
            ObjectInputStream in;
            List<User> tmp = new ArrayList<>();
            File f = new File(USER_FILE);
            if (!f.exists()) {
                out = new ObjectOutputStream(
                        new FileOutputStream(f));
                out.writeObject(users);
                out.close();
            } else {
                in = new ObjectInputStream(
                        new FileInputStream(f));
                tmp = (List<User>) in.readObject();
                int temp_size = tmp.size();
                int size = users.size();
                final int idsize = temp_size + size;
                for (int i = 0; i < size; i++) {
                    tmp.add(users.get(i));
                }
                for (int i = 0; i < idsize; i++) {
                    tmp.get(i).setId(i + 1);
                }
                in.close();
                out = new ObjectOutputStream(new FileOutputStream(USER_FILE));
                out.writeObject(tmp);
                out.close();
            }
            return true;

        } catch (BusinessException | FileNotFoundException e) {
            println(getString("userIO.write.error"));
        } catch (IOException | ClassNotFoundException e) {
            println(getString("userIO.read.error"));
        }
        return false;
    }

    public  boolean readUsers()throws BusinessException{
        try {
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(USER_FILE));
            users= (List<User>)in.readObject();
            System.out.println(users.size());
            for(User user:users){
                System.out.println(user.toString());
            }
            in.close();
            return true;
        } catch (IOException | ClassNotFoundException e) {
            println(getString("userIO.read.error"));
        }
        return false;
    }

    public void addUsers(User user){
        user.setId(users.size()+1);
        users.add(user);
        System.out.println("添加用户成功!\n序号是: "+user.getId()+"\n用户名是: "+user.getUsername());

    }

    public User findbyUsernameandPassword(String username,String password){
        UserIO ui=new UserIO();
        for(User obj:ui.getTmp()){
            if(obj.getUsername().equals(username) && obj.getPassword().equals(password)){
                return obj;
            }
        }
        return null;
    }

    public  List<User> getTmp() {
        return tmp;
    }

    private static void writetest(){
        try {
            File f=new File(new File(".").getAbsolutePath()+"/"+"test.obj");
            ObjectOutputStream out=new ObjectOutputStream
                    (new FileOutputStream(f));
            out.writeObject(new String("test"));
            out.close();
            System.out.println(f.getPath());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
